package com.youkeyi.ddy.cloud.basic.crm.follow.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.crm.follow.entity.SysCustomerOrderFollowUp;
import com.youkeyi.ddy.cloud.basic.crm.follow.service.SysCustomerOrderFollowUpService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.CrmCustomerOrderFollowUpCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.dto.CrmCustomerOrderFollowUpListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.dto.CrmCustomerOrderFollowUpListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.param.CrmCustomerOrderFollowUpCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.param.CrmCustomerOrderFollowUpDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.param.CrmCustomerOrderFollowUpQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.param.CrmCustomerOrderFollowUpUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 客户订单跟进情况 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-03-04
 */
@Service
public class SysCustomerOrderFollowUpController implements CrmCustomerOrderFollowUpCloudService {

    @Autowired
    private SysCustomerOrderFollowUpService sysCustomerOrderFollowUpService;

    @Override
    public CrmCustomerOrderFollowUpListCloudDto query(CrmCustomerOrderFollowUpQueryCloudParam param) {
        CrmCustomerOrderFollowUpListCloudDto dto = new CrmCustomerOrderFollowUpListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getCustomerFollowUpPrimaryKeys())) {
            param.getCustomerFollowUpPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCorpKeys())) {
            param.getCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCustomerKeys())) {
            param.getCustomerKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getOrderKeys())) {
            param.getOrderKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getStatuses())) {
            param.getStatuses().removeAll(Collections.singleton(null));
        }

        QueryWrapper<SysCustomerOrderFollowUp> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getCustomerFollowUpPrimaryKeys()), SysCustomerOrderFollowUp::getPrimaryKey, param.getCustomerFollowUpPrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getCorpKeys()), SysCustomerOrderFollowUp::getCorpKey, param.getCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getCustomerKeys()), SysCustomerOrderFollowUp::getCustomerKey, param.getCustomerKeys())
                .in(CollectionUtils.isNotEmpty(param.getOrderKeys()), SysCustomerOrderFollowUp::getOrderKey, param.getOrderKeys())
                .in(CollectionUtils.isNotEmpty(param.getStatuses()), SysCustomerOrderFollowUp::getStatus, param.getStatuses())
                .ne(SysCustomerOrderFollowUp::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;

        Page<SysCustomerOrderFollowUp> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        wrapper.lambda().orderByDesc(SysCustomerOrderFollowUp::getId);
        Page<SysCustomerOrderFollowUp> customerOrderFollowUpList = sysCustomerOrderFollowUpService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(customerOrderFollowUpList.getRecords())) {
            dto.setTotal(customerOrderFollowUpList.getTotal());

            List<CrmCustomerOrderFollowUpListItemCloudDto> items = new ArrayList<>();
            customerOrderFollowUpList.getRecords().forEach(customerOrderFollowUp -> {
                CrmCustomerOrderFollowUpListItemCloudDto item = new CrmCustomerOrderFollowUpListItemCloudDto();
                item.setCustomerFollowUpPrimaryKey(customerOrderFollowUp.getPrimaryKey());
                item.setCorpKey(customerOrderFollowUp.getCorpKey());
                item.setCustomerKey(customerOrderFollowUp.getCustomerKey());
                item.setCustomerName(customerOrderFollowUp.getCustomerName());
                item.setOrderKey(customerOrderFollowUp.getOrderKey());
                item.setFollowUp(customerOrderFollowUp.getFollowUp());
                item.setFollowUpDate(customerOrderFollowUp.getFollowUpDate() != null ? customerOrderFollowUp.getFollowUpDate().getTime() : null);
                item.setNextFollowUpRemindDate(customerOrderFollowUp.getNextFollowUpRemindDate() != null ? customerOrderFollowUp.getNextFollowUpRemindDate().getTime() : null);
                item.setRemindStart(customerOrderFollowUp.getRemindStart());
                item.setStatus(customerOrderFollowUp.getStatus());
                item.setRemark(customerOrderFollowUp.getRemark());
                item.setServerDate(customerOrderFollowUp.getServerDate() != null ? customerOrderFollowUp.getServerDate().getTime() : null);
                item.setAssistingPersonnelKey(customerOrderFollowUp.getAssistingPersonnelKey());
                item.setAssistingPersonnelName(customerOrderFollowUp.getAssistingPersonnelName());
                item.setIsDeleted(customerOrderFollowUp.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(customerOrderFollowUp.getCreateTime() != null ? customerOrderFollowUp.getCreateTime().getTime() : null);
                item.setCreateUserKey(customerOrderFollowUp.getCreateUserKey());
                item.setCreateUserName(customerOrderFollowUp.getCreateUserName());
                item.setUpdateTime(customerOrderFollowUp.getUpdateTime() != null ? customerOrderFollowUp.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(customerOrderFollowUp.getUpdateUserKey());
                item.setUpdateUserName(customerOrderFollowUp.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(CrmCustomerOrderFollowUpCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param
                || StringUtils.isBlank(param.getCorpKey()) || StringUtils.isBlank(param.getOrderKey())
                || StringUtils.isBlank(param.getCustomerKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        SysCustomerOrderFollowUp customerOrderFollowUp = new SysCustomerOrderFollowUp();
        customerOrderFollowUp.setPrimaryKey(SimpleUUidUtils.getUuid());
        customerOrderFollowUp.setCorpKey(param.getCorpKey());
        customerOrderFollowUp.setCustomerKey(param.getCustomerKey());
        customerOrderFollowUp.setCustomerName(param.getCustomerName());
        customerOrderFollowUp.setOrderKey(param.getOrderKey());
        customerOrderFollowUp.setFollowUp(param.getFollowUp());
        customerOrderFollowUp.setFollowUpDate(null != param.getFollowUpDate() ? new Date(param.getFollowUpDate()) : null);
        customerOrderFollowUp.setNextFollowUpRemindDate(param.getNextFollowUpRemindDate() != null ? new Date(param.getNextFollowUpRemindDate()) : null);
        customerOrderFollowUp.setRemindStart(param.getRemindStart());
        customerOrderFollowUp.setStatus(param.getStatus());
        customerOrderFollowUp.setRemark(param.getRemark());
        customerOrderFollowUp.setServerDate(param.getServerDate() != null ? new Date(param.getServerDate()) : null);
        customerOrderFollowUp.setAssistingPersonnelKey(param.getAssistingPersonnelKey());
        customerOrderFollowUp.setAssistingPersonnelName(param.getAssistingPersonnelName());
        customerOrderFollowUp.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        customerOrderFollowUp.setCreateUserKey(param.getCreateUserKey());
        customerOrderFollowUp.setCreateUserName(param.getCreateUserName());
        customerOrderFollowUp.setCreateTime(new Date());
        boolean save = sysCustomerOrderFollowUpService.save(customerOrderFollowUp);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(customerOrderFollowUp.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(CrmCustomerOrderFollowUpUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getCustomerFollowUpPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        UpdateWrapper<SysCustomerOrderFollowUp> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(SysCustomerOrderFollowUp::getPrimaryKey, param.getCustomerFollowUpPrimaryKey());
        SysCustomerOrderFollowUp customerOrderFollowUp = new SysCustomerOrderFollowUp();
        customerOrderFollowUp.setPrimaryKey(param.getCustomerFollowUpPrimaryKey());
        customerOrderFollowUp.setFollowUp(param.getFollowUp());
        customerOrderFollowUp.setFollowUpDate(null != param.getFollowUpDate() ? new Date(param.getFollowUpDate()) : null);
        customerOrderFollowUp.setNextFollowUpRemindDate(param.getNextFollowUpRemindDate() != null ? new Date(param.getNextFollowUpRemindDate()) : null);
        customerOrderFollowUp.setRemindStart(param.getRemindStart());
        customerOrderFollowUp.setStatus(param.getStatus());
        customerOrderFollowUp.setRemark(param.getRemark());
        customerOrderFollowUp.setServerDate(param.getServerDate() != null ? new Date(param.getServerDate()) : null);
        customerOrderFollowUp.setAssistingPersonnelKey(param.getAssistingPersonnelKey());
        customerOrderFollowUp.setAssistingPersonnelName(param.getAssistingPersonnelName());
        customerOrderFollowUp.setUpdateUserKey(param.getUpdateUserKey());
        customerOrderFollowUp.setUpdateUserName(param.getUpdateUserName());
        customerOrderFollowUp.setUpdateTime(new Date());
        boolean save = sysCustomerOrderFollowUpService.update(customerOrderFollowUp, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        return rc;
    }

    @Override
    public ResponseContent delete(CrmCustomerOrderFollowUpDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || CollectionUtils.isEmpty(param.getCustomerFollowUpPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        UpdateWrapper<SysCustomerOrderFollowUp> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .in(SysCustomerOrderFollowUp::getPrimaryKey, param.getCustomerFollowUpPrimaryKeys());

        SysCustomerOrderFollowUp customerOrderFollowUp = new SysCustomerOrderFollowUp();
        customerOrderFollowUp.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
        customerOrderFollowUp.setUpdateUserKey(param.getUpdateUserKey());
        customerOrderFollowUp.setUpdateUserName(param.getUpdateUserName());
        customerOrderFollowUp.setUpdateTime(new Date());
        boolean save = sysCustomerOrderFollowUpService.update(customerOrderFollowUp, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
        }
        return rc;
    }
}
